﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using THANHTUNGO.Portal.Components.Security;
using THANHTUNGO.Portal.Components.Settings;
using THANHTUNGO.Portal.Components.UI;

namespace THANHTUNGO.Portal
{
    public partial class ModuleTitle : UserControl
    {
        private string _AddUrl = string.Empty;
        private string _ManagerUrl = string.Empty;
        private string _SettingUrl = string.Empty;
        private string _PropertyUrl = string.Empty;
        private string _SecurityUrl = string.Empty;

        [Browsable(true)]
        [Category("Appearance")]
        public string AddUrl
        {
            set { this._AddUrl = value; }
            get { return this._AddUrl; }
        }

        [Browsable(true)]
        [Category("Appearance")]
        public string ManagerUrl
        {
            set { this._ManagerUrl = value; }
            get { return this._ManagerUrl; }
        }

        [BrowsableAttribute(true)]
        [Category("Appearance")]
        public string SettingUrl
        {
            set { this._SettingUrl = value; }
            get { return this._SettingUrl; }
        }

        [Browsable(true)]
        [Category("Appearance")]
        public string PropertyUrl
        {
            set { this._PropertyUrl = value; }
            get { return this._PropertyUrl; }
        }

        [Browsable(true)]
        [Category("Appearance")]
        public string SecurityUrl
        {
            set { this._SecurityUrl = value; }
            get { return this._SecurityUrl; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Obtain PortalSettings from Current Context
                PortalSettings portalSettings = (PortalSettings) HttpContext.Current.Items["PortalSettings"];

                // Obtain reference to parent portal module
                PortalModuleControl portalModule = (PortalModuleControl) this.Parent;

                // Display Modular Title Text and Edit Buttons			
                lblModuleTitle.Text = portalModule.Module.Title;
                
                if (portalModule.IsAddable || portalModule.IsEditable || portalModule.IsDeleteable)
                    pnlTitle.Visible = true;
                else
                    pnlTitle.Visible = portalModule.Module.ShowTitle;
                
                // Display the Edit button if the parent portalmodule has configured the PortalModuleTitle User Control
                // to display it -- and the current client has edit access permissions
                if ((portalSettings.AlwaysShowEditButton) || (PortalSecurity.IsInRoles("1")))
                {

                    if (this._AddUrl != null)
                    {                        
                        AddButton.ImageUrl = "~/images/icons/addb.gif";
                        AddButton.NavigateUrl = this._AddUrl;
                        AddButton.ToolTip = Global.RM.GetString("SYSTEM_ADDNEW");
                        AddButton.Visible = PortalSecurity.HasAddPermission(portalModule.ModuleID);
                        if (AddButton.Visible) AddButton.Visible = this.AddUrl.Length > 0;
                    }
                    if (this._ManagerUrl != null)
                    {
                        ManagerButton.ImageUrl = "~/images/icons/addb.gif";
                        ManagerButton.NavigateUrl = this._ManagerUrl;
                        ManagerButton.ToolTip = Global.RM.GetString("SYSTEM_ADDNEW");
                        ManagerButton.Visible = PortalSecurity.HasManagerPermission(portalModule.ModuleID);
                        if (ManagerButton.Visible) ManagerButton.Visible = this.ManagerUrl.Length > 0;
                    }

                    if (this._SettingUrl != null)
                    {
                        SettingButton.ImageUrl = "~/images/icons/plugin.gif";
                        SettingButton.NavigateUrl = this._SettingUrl;
                        SettingButton.ToolTip = Global.RM.GetString("SYSTEM_SETTING");
                        SettingButton.Visible = PortalSecurity.HasPropertyPermission(portalModule.ModuleID);
                    }

                    if (this._PropertyUrl != null)
                    {
                        PropertyButton.ImageUrl = "~/images/icons/Properties.gif";
                        PropertyButton.NavigateUrl = this._PropertyUrl;
                        PropertyButton.ToolTip = Global.RM.GetString("SYSTEM_PROPERTY");
                        PropertyButton.Visible = PortalSecurity.HasPropertyPermission(portalModule.ModuleID);                        
                    }

                    if (this._SecurityUrl != null)
                    {
                        SecurityButton.ImageUrl = "~/images/icons/Security.gif";
                        SecurityButton.NavigateUrl = this._SecurityUrl;
                        SecurityButton.ToolTip = Global.RM.GetString("SYSTEM_PERMISSION");
                        SecurityButton.Visible = PortalSecurity.HasPropertyPermission(portalModule.ModuleID);
                    }
                }
            }
        }
    }
}